﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfCmfInnerPayPwd : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                if (!msBsc.SvRef.UserInfo.Online)
                {
                    this.ExecJScript("window.location.href='/mp_UserLogin.htm'");
                    return;

                }
                System.Data.DataSet ds = objCBizCustomerRegister.SelectById(msBsc.SvRef.UserInfo.Operator);

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript("window.location.href='/mp_UserLogin.htm'");
                    return;
                }

                if (ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"] == DBNull.Value
                 || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"].ToString()))
                {
                    this.ExecJScript(@"alert(""您尚未设置支付密码。请设置支付密码后，再进行支付账号绑定。"");window.location.href=""/ms_SetInnerPayPwd.htm?ReturnUrl=/ms_ClearBalanceInfo.htm""");
                    return;
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "CmfPayPwd")
            {
                CmfInnerPayPwd();
            }
        }

        private void CmfInnerPayPwd()
        {
            string validCode = this.GetParamHtValue("validCode");
            if (string.IsNullOrEmpty(validCode) ||
                validCode != (string)GetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, "2"))
            {
                this.ExecJScript(@"ShowErrMsg(""txtCmfCode"", ""notice-msg-validcode"", ""验证码错误或已过期，请重新输入"");");
                return;
            }

            string innerPwd = this.GetParamHtValue("userPayPwd");

            if (string.IsNullOrEmpty(innerPwd))
            {
                this.ExecJScript(@"ShowErrMsg(""pwdUserPayPwd"", ""notice-msg-aliaccount"", ""支付密码不能为空"");");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                System.Data.DataSet ds = objCBizCustomerRegister.DataObj.QueryBySQL("SELECT CUSTOMER_PAY_INNERPWD FROM DJK_CUSTOMER_REGISTER WHERE CUSTOMER_REGISTER_ID='" + msBsc.SvRef.UserInfo.Operator + "'");

                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript(@"alert(""用户登录信息有误，请重新登陆"");window.location.href=""/wp_UserLogin.htm""");
                    return;
                }

                if (ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"].ToString() != msBsc.AppLogic.Cfg.DesCry.encMe(innerPwd, "zbhpass"))
                {
                    this.ExecJScript(@"ShowErrMsg(""pwdUserPayPwd"", ""notice-msg-aliaccount"", ""支付密码错误"");");
                    return;
                }

                msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd = true;
                this.ExecJScript(@"TurnToReturnUrl();");
            }
        }
    }
}